﻿using System.Data;
using System.Transactions;
using Microsoft.Practices.Unity;
using SpectrumAnalyser.Common;
using SpectrumAnalyser.Common.InstanceContext;
using SpectrumAnalyser.Data.Common.Contracts;

namespace SpectrumAnalyser.Data.Common
{
    public class RepositoryFactoryBase : IRepositoryFactory
    {
        protected readonly IUnityContainer _unityContainer;
        private readonly DbManagerBase _dbManagerBase;

        public RepositoryFactoryBase(IUnityContainer unityContainer, InstanceContext instanceContext)
        {
            _unityContainer = unityContainer;
            RegisterInstance(instanceContext);

            _dbManagerBase = unityContainer.Resolve<DbManagerBase>();
        }

        public virtual void RegisterInstance(InstanceContext instanceContext)
        {
            
        }

        public T Create<T>() where T : IRepository
        {
            return _unityContainer.Resolve<T>();
        }

        public ITransaction BeginTransaction()
        {
            return new TransactionWrapper(new TransactionScope(), _dbManagerBase);
        }
    }
}
